var config = require("./config.json");
var pktSizeCDF = require(config.pktSizeCDF);
var flowSizeCDF = require(config.flowSizeCDF);
var getRandomCDF = require("./getRandomCDF.js");

function getFlowFeature() {
	var result = []; //结果数组，每一行是flow的特征
	//得到源目主机号
	var s = Math.floor(Math.random() * config.hostNum);
	var d;
	do{
		d = Math.floor(Math.random() * config.hostNum);
	}while(d == s);

	result.push(s);
	result.push(d);	
	//得到端口号, 端口号范围都限定为0-100
	var s_port = Math.floor(Math.random() * config.portRange);
	var d_port = Math.floor(Math.random() * config.portRange);
	
	result.push(s_port);
	result.push(d_port);
	//得到前三个报文大小
	var pktSize = [];
	for(var i = 0; i < config.pktNum; i++) {
		pktSize.push(Math.ceil(getRandomCDF(pktSizeCDF)));
	}

	result = result.concat(pktSize);
	//产生流的大小，得到流大小对应的区间, 单位byte。
	var flowSize = Math.ceil(getRandomCDF(flowSizeCDF));
	var flowSizeLevel = 0;
	var flowSizeRange = flowSizeCDF.range;
	for(var i = flowSizeRange.length - 1; i >= 0; i--) {
		if(flowSize >= flowSizeRange[i]) {
			flowSizeLevel = i;
			break;
		}
	}

	console.log("flowSize: " + flowSize, "level: " + flowSizeLevel);

	result.push(flowSizeLevel);

	result = result.join(",");
	return result;
}

module.exports = getFlowFeature;